package com.wm.web.action;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;

import com.wm.bean.Address;
import com.wm.bean.Collect;
import com.wm.bean.User;
import com.wm.comm.Constant;
import com.wm.comm.MD5;
import com.wm.service.AddressService;
import com.wm.service.CollectService;
import com.wm.service.UserService;
import com.wm.web.servlet.AdminSession;
import com.wm.web.servlet.UserSession;

public class AddressAction extends BaseAction implements SessionAware {
	private static final Log logger = LogFactory.getLog(AddressAction.class);
	
	private AddressService addressService;

	public AddressService getAddressService() {
		return addressService;
	}

	public void setAddressService(AddressService addressService) {
		this.addressService = addressService;
	}
	
	private Map session;

	public void setSession(Map session) {
		this.session = session;
	}

	public Map getSession() {
		return session;
	}
	
	private long id;					//主键ID
	private String linkman;				//联系人
	private String Phone;				//电话
	private String backPhone;			//备用电话
	private String address;				//联系地址
	private int isDefault;			//是否是默认地址，0不是1是
	private List list;
	private Address addr;
	private String error;

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error = error;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getLinkman() {
		return linkman;
	}

	public void setLinkman(String linkman) {
		this.linkman = linkman;
	}

	public String getPhone() {
		return Phone;
	}

	public void setPhone(String phone) {
		Phone = phone;
	}

	public String getBackPhone() {
		return backPhone;
	}

	public void setBackPhone(String backPhone) {
		this.backPhone = backPhone;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public int getIsDefault() {
		return isDefault;
	}

	public void setIsDefault(int isDefault) {
		this.isDefault = isDefault;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}
	
	public Address getAddr() {
		return addr;
	}

	public void setAddr(Address addr) {
		this.addr = addr;
	}

	public String execute() {
		try {
			if(this.getSession().get(Constant.USER_SESSION_KEY)!=null){
				return this.executeMethod(this.getAction());
			}
			return "default";
		} catch (Exception e) {
			logger.error(e);
			return INPUT;
		}
	}

	/**
	 * 初始页
	 */
	public String index(){
		UserSession us = (UserSession)this.getSession().get(Constant.USER_SESSION_KEY);
		this.setList(this.getAddressService().findAddressByUserId(us.getUid()));
		this.setAction("index");
		if(this.getError()!=null&&!this.getError().equals("")){
			this.addFieldError("error", this.getText(this.getError()));
		}
		return "index";
	}
	
	/**
	 * 添加地址
	 * @return
	 */
	public String save(){
		if(this.getLinkman()==null ||this.getLinkman().trim().equals("") ||this.getLinkman().length()>20){
			this.setError("error.linkman");
			this.setAction("index");
			return "indexAction";
		}
		
		if(this.getPhone()==null ||this.getPhone().trim().equals("")||this.getPhone().length()>20){
			this.setError("error.phone");
			this.setAction("index");
			return "indexAction";
		}
		
		if(!checkPhone(this.getPhone())){
			this.setError("error.phone");
			this.setAction("index");
			return "indexAction";
		}
		
		if(this.getBackPhone()!=null && !this.getBackPhone().trim().equals("") && this.getBackPhone().length()>20){
			this.setError("error.backPhone");
			this.setAction("index");
			return "indexAction";
		}
		
		if(this.getBackPhone()!=null && !this.getBackPhone().trim().equals("") && !checkPhone(this.getBackPhone())){
			this.setError("error.backPhone");
			this.setAction("index");
			return "indexAction";
		}
		
		if(this.getAddress()==null || this.getAddress().trim().equals("") || this.getAddress().length()>200){
			this.setError("error.address");
			this.setAction("index");
			return "indexAction";
		}
		
		UserSession us = (UserSession)this.getSession().get(Constant.USER_SESSION_KEY);
		Address a = new Address();
		a.setUserId(us.getUid());
		a.setLinkman(this.getLinkman());
		a.setPhone(this.getPhone());
		a.setBackPhone(this.getBackPhone());
		a.setAddress(this.getAddress());
		a.setIsDefault(this.getIsDefault());
		a.setAddTime(new Date());
		this.getAddressService().saveAddress(a);
		this.setAction("index");
		return "indexAction";
	}
	
	/**
	 * 删除
	 * @return
	 */
	public String remove(){
		if(this.getId()>0){
			this.getAddressService().removeAddress(this.getId());
		}
		this.setAction("index");
		return "indexAction";
	}
	
	/**
	 * 修改默认
	 * @return
	 */
	public String updateIsDefault(){
		if(this.getId()>0){
			UserSession us = (UserSession)this.getSession().get(Constant.USER_SESSION_KEY);
			Address a = this.getAddressService().findAddressByIsUserIdDefault(us.getUid());
			if(a!=null){
				a.setIsDefault(0);
				this.getAddressService().updateAddress(a);
			}
			a = this.getAddressService().findAddressById(this.getId());
			if(a!=null){
				a.setIsDefault(1);
				this.getAddressService().updateAddress(a);
			}
		}
		this.setAction("index");
		return "indexAction";
	}
	
	/**
	 * 编辑页
	 */
	public String content(){
		UserSession us = (UserSession)this.getSession().get(Constant.USER_SESSION_KEY);
		this.setList(this.getAddressService().findAddressByUserId(us.getUid()));
		if(this.getId()>0){
			this.setAddr(this.getAddressService().findAddressById(this.getId()));
		}
		if(this.getError()!=null&&!this.getError().equals("")){
			this.addFieldError("error", this.getText(this.getError()));
		}
		return "update";
	}
	
	/**
	 * 编辑
	 * @return
	 */
	public String update(){
		if(this.getLinkman()==null ||this.getLinkman().trim().equals("") ||this.getLinkman().length()>20){
			this.setAction("content");
			this.setError("error.linkman");
			return "contentAction";
		}
		
		if(this.getPhone()==null ||this.getPhone().trim().equals("")||this.getPhone().length()>20){
			this.setAction("content");
			this.setError("error.phone");
			return "contentAction";
		}
		
		if(!checkPhone(this.getPhone())){
			this.setAction("content");
			this.setError("error.phone");
			return "contentAction";
		}
		
		if(this.getBackPhone()!=null && !this.getBackPhone().trim().equals("") && this.getBackPhone().length()>20){
			this.setAction("content");
			this.setError("error.backPhone");
			return "contentAction";
		}
		
		if(this.getBackPhone()!=null && !this.getBackPhone().trim().equals("") && !checkPhone(this.getBackPhone())){
			this.setAction("content");
			this.setError("error.backPhone");
			return "contentAction";
		}
		
		if(this.getAddress()==null || this.getAddress().trim().equals("") || this.getAddress().length()>200){
			this.setAction("content");
			this.setError("error.address");
			return "contentAction";
		}
		
		Address a = this.getAddressService().findAddressById(this.getId());
		if(a!=null){
			UserSession us = (UserSession)this.getSession().get(Constant.USER_SESSION_KEY);
			a.setUserId(us.getUid());
			a.setLinkman(this.getLinkman());
			a.setPhone(this.getPhone());
			a.setBackPhone(this.getBackPhone());
			a.setAddress(this.getAddress());
			this.getAddressService().updateAddress(a);
		}
		
		this.setAction("content");
		return "contentAction";
	}
	
	public static boolean checkPhone(String phone) {
	  String regex = "(^(0\\d{2,3})?-?([2-9]\\d{6,7})(-\\d{1,5})?$)|(^((\\(\\d{3}\\))|(\\d{0}))?(13|14|15|18)\\d{9}$)|(^(400|800)\\d{7}(-\\d{1,4})?$)|(^(95013)\\d{6,8}$)";
	  Pattern p = Pattern.compile(regex);
	  Matcher m = p.matcher(phone);
	  return m.matches();
	}
}
